package com.yjm.test.Spring5.ch4.factoryBean;

import java.security.MessageDigest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class MessageDigester  implements ApplicationListener<ApplicationContextEvent> {
	private MessageDigest digest1;
	private MessageDigest digest2;
	

	public MessageDigest getDigest1() {
		return digest1;
	}
	@Autowired
	@Qualifier("digest1")
	public void setDigest1(MessageDigest digest1) {
		this.digest1 = digest1;
	}

	public MessageDigest getDigest2() {
		return digest2;
	}
	@Autowired
	@Qualifier("digest2")
	public void setDigest2(MessageDigest digest2) {
		this.digest2 = digest2;
	}
	
	public void digest(String msg) {
		System.out.println("Using digest1");
		digest(msg, digest1);
		System.out.println("Using digest2");
		digest(msg, digest2);
	}
	public void digest(String msg, MessageDigest d) {
		System.out.println("Using algorithm: " + d.getAlgorithm());
		d.reset();
		byte[] bytes = msg.getBytes();
		byte[] out = d.digest(bytes);
		System.out.println(out);
	}
	
	public static void testXml() {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:spring/ch4/message-digester-factory-bean.xml");
		ctx.refresh();
		
		MessageDigester digester = ctx.getBean(MessageDigester.class);
		digester.digest("Hello World!");
		//访问 FactoryBean 很简单，在调用 getBeanO时用“＆” 符 号作为 bean 名称的前缀即可
		MessageDigestFactoryBean fb = (MessageDigestFactoryBean) ctx.getBean("&shaDigest");
		System.out.println(fb);
		
		ctx.close();
	}
	
	@Configuration
	@ComponentScan(basePackages="com.yjm.test.Spring5.ch4.factoryBean")
	public static class MessageDigesterConfig {
		@Bean
		MessageDigestFactoryBean digest1() {
			MessageDigestFactoryBean factoryOne = new MessageDigestFactoryBean();
			factoryOne.setAlgorithmName("SHA1");
			return factoryOne;
		}
		@Bean(name="digest2")
		MessageDigestFactoryBean getDigest2() {
			MessageDigestFactoryBean factoryOne = new MessageDigestFactoryBean();
			//factoryOne.setAlgorithmName("SHA1");
			return factoryOne;
		}
		
		@Bean
		MessageDigester digester() throws Exception {
			MessageDigester md = new MessageDigester();
			md.setDigest1(digest1().getObject());
			md.setDigest2(getDigest2().getObject());
			return md;
		}
	}
	public static void testAnnotation() {
		GenericApplicationContext ctx = new AnnotationConfigApplicationContext(MessageDigesterConfig.class);
		
		MessageDigester digester = (MessageDigester) ctx.getBean("messageDigester");
		digester.digest("Hello World!");
		//访问 FactoryBean 很简单，在调用 getBeanO时用“＆” 符 号作为 bean 名称的前缀即可
		MessageDigestFactoryBean fb = (MessageDigestFactoryBean) ctx.getBean("&digest1");
		System.out.println(fb);
		
		ctx.close();
	}


	public static void main(String[] args) { 
		testXml();
		System.out.println("\n\n\n\n\n\n");
		testAnnotation();
	}


	@Override
	public void onApplicationEvent(ApplicationContextEvent event) {
		System.out.println("get application context event: " + event.toString());
	}

}
